﻿using H;
using H.Daos;
using H.Eyes;
using H.Hands;
using H.Kernel.Utils;
using H.Logs;
using H.QuickDoes;
using H.Types;
using H.Utils;
using System;

namespace Test
{

    /// <summary>
    /// 此时DML和KML是否能够正常工作
    /// </summary>
    public class Test3 : IApp
    {
        private class QDC : IQDContext
        {
        }

        public static void Main(string[] args)
        {
            //IEnumerable<string[]> enumerable()
            //{
            //    foreach (var i in args)
            //    {
            //        var tmpPiece = i.Split(new char[] { '=' }, 2);
            //        if (tmpPiece.Length == 2)
            //        {
            //            yield return tmpPiece;
            //        }
            //    }
            //}

            //Dictionary<string, string> argDict = enumerable().ToDictionary(keys => keys[0], keys => keys[1]);

            //if (argDict.TryGetValue(""))
            //{

            //}
            ILogCollector logCollector = HService.DefaultLoggerFactory.Value.GetInstance();
            logCollector.AddListener(HService.DefaultLoggerFactory.Value.CreateFileLogListener("Test"));
            logCollector.AddListener(HService.DefaultLoggerFactory.Value.CreateLocalLogListener());
            logCollector.AddListener(HService.DefaultLoggerFactory.Value.CreateWebLogListener("127.0.0.1", 5566, "Test3"));
            ILog log = logCollector.GetInstance(nameof(Test3));

            DateTime now = DateTime.Now;
            IDao dao = HService.DefaultDaoFactory.Value.CreateInstance(HService.GetEnvPath("main.hdb"), null, false);
            log.Debug("已经成功加载主数据库文件");
            log.Debug("到Loader之前耗时:" + (now - DateTime.Now).TotalMilliseconds);
            now = DateTime.Now;

            ResLoader loader = new ResLoader(dao);

            IQuickDo<QDC> quickDo = HService.DefaultQuickDoFactory.Value.CreateInstance<QDC>();

            log.Debug("到Eye之前耗时:" + (now - DateTime.Now).TotalMilliseconds);
            now = DateTime.Now;
            IEye eye = HService.DefaultEyeFactory.Value.CreateInstance();
            eye.DefaultOption.DeletaColor = new RGB(0x202020);

            log.Debug("到Hand之前耗时:" + (now - DateTime.Now).TotalMilliseconds);
            now = DateTime.Now;
            IHand hand = HService.DefaultHandFactory.Value.CreateInstance();

            log.Debug("到ISee之前耗时:" + (now - DateTime.Now).TotalMilliseconds);
            now = DateTime.Now;
            ISee findNotepadTask = eye.FindPic(loader.LoadImgRes("Test", "NotepadIcon"));
            ISee findNotepad = eye.FindPic(loader.LoadImgRes("Test", "NotepadMain"));
            ISee findRunTask = eye.FindPic(loader.LoadImgRes("Test", "RunBoxIcon"));

            IKeyBoard KeyCtrlA = hand.CreateKeys(HandKey.Ctrl, HandKey.A);
            IKeyBoard KeyBackspace = hand.CreateKey(HandKey.Backspace);
            IKeyBoard KeyEnter = hand.CreateKey(HandKey.Enter);
            IKeyBoard KeyWinR = hand.CreateKeys(HandKey.Win, HandKey.R);
            IMouse LeftMouse = hand.LeftMouse();

            Rect workRect = RectUtils.GetWorkAreaRect();
            Rect taskBarRect = RectUtils.GetTaskBarRect();

            Rect inputRect = Rect.FromLTRB(56, 55, 102, 68);

            log.Debug("到初始化QD之前耗时:" + (now - DateTime.Now).TotalMilliseconds);
            now = DateTime.Now;
            log.Debug("加载相关数据成功");

            quickDo.SetAction(1, qd =>
             {
                 if (ProcessUtil.Exists("notepad"))
                 {
                     log.Debug("存在已经启动的记事本,正在尝试激活记事本");
                     if (findNotepad.See(workRect, out Pos matchPoint) != -1)
                     {
                         log.Debug("已激活记事本");
                         return 2;
                     }
                     else
                     {
                         log.Debug("查到记事本图标");
                         if (findNotepadTask.See(taskBarRect, out matchPoint) != -1)
                         {
                             log.Debug("找到记事本图标,准备点击");
                             LeftMouse.Click(matchPoint);
                         }
                         return false;
                     }
                 }
                 else
                 {
                     log.Debug("没有找到已经启动的记事本,正在尝试打开记事本");
                     KeyWinR.Click();
                     return 4;
                 }
             }, 10, 2);

            //找到记事本,准备在记事本中写入点东西
            quickDo.SetAction(2, qd =>
            {
                if (findNotepad.See(workRect, out Pos matchPoint) != -1)
                {
                    if (LeftMouse.Click(matchPoint.Add(20, 90)))
                    {
                        qd.Delay(800, 1000);
                        KeyCtrlA.Click();
                        qd.Delay(200, 300);
                        KeyBackspace.Click();
                        qd.Delay(800, 1000);
                        hand.InputString("Hello world");
                        KeyEnter.Click();
                        qd.Delay(800, 1000);
                        int r = RandomUtil.Next(5);
                        hand.InputString($"即将输入{r}个空行");
                        while (r-- >= 0)
                        {
                            KeyEnter.Click();
                        }
                        hand.InputString("宝义是傻逼,哈哈");
                        KeyEnter.Click();
                        hand.InputString("Test Over!!!");
                        KeyEnter.Click();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return 1;
                }

            }, 2, null);


            quickDo.SetAction(4, qd =>
            {
                if (findRunTask.See(workRect, out Pos matchPoint) != -1)
                {
                    Rect rect = inputRect;
                    rect.Offset(matchPoint);
                    LeftMouse.Click(rect);
                    qd.Delay(500);
                    KeyCtrlA.Click();
                    qd.Delay(500);
                    KeyBackspace.Click();
                    qd.Delay(500);
                    hand.InputString("notepad");
                    qd.Delay(800);
                    KeyEnter.Click();
                    return 2;
                }
                else
                {
                    log.Error("无法打开运行窗口");
                    return false;
                }
            }, 10, null);

            log.Debug("到执行QD之前耗时:" + (now - DateTime.Now).TotalMilliseconds);
            now = DateTime.Now;
            log.Debug("结果:" + quickDo.DoAction(1));
        }
    }
}
